package com.centaline.androidsalesblog.activities.mine;

import android.content.Intent;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.SysMsgAdapter;
import com.centaline.androidsalesblog.api.usercenter.SysMsgListApi;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.model.businissobject.SysMsgBo;
import com.centaline.androidsalesblog.model.responseobject.SysMsgListRo;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centanet.framework.interfaces.IRecycleViewListener;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.ArrayList;

/**
 * 系统消息列表
 * Created by Ruan on 16/2/1.
 */

public class SysMsgListActivity extends BaseActivity {

    private MRecyclerView mRecyclerView;
    private int iRefreshType;
    private ArrayList<SysMsgBo> list = new ArrayList<>();

    private SysMsgListApi sysMsgListApi;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_sys_msg_list;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_sys_msg_list);
        mRecyclerView = (MRecyclerView) findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void initViews() {
        mRecyclerView.setIRecycleViewListener(new IRecycleViewListener() {
            @Override
            public void downRefresh() {
                iRefreshType = 1;
                sysMsgListApi.setFirstIndex(0);
                request();
            }

            @Override
            public void upRefresh() {
                iRefreshType = 2;
                sysMsgListApi.setFirstIndex(list.size());
                request();
            }

            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(SysMsgListActivity.this, MsgDetailActivity.class));
            }
        });
        mRecyclerView.setAdapter(new SysMsgAdapter(this, list));

        sysMsgListApi = new SysMsgListApi(SysMsgListActivity.class, SysMsgListRo.class, new IResponseListener<SysMsgListRo>() {
            @Override
            public void onResponse(SysMsgListRo sysMsgListRo) {
                ArrayList<SysMsgBo> temp = sysMsgListRo.getList();
                if (iRefreshType == 1) {
                    list.clear();
                }
                if (temp == null || temp.size() == 0) {
                    mRecyclerView.stopRefresh(false);
                } else {
                    list.addAll(temp);
                    mRecyclerView.stopRefresh(temp.size() == 10);
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                netWorkUnable();
            }
        });
        mRecyclerView.startRefresh();
    }

    private void request() {
        sysMsgListApi.startRequest();
    }
}
